package com.bs.manager04.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bs.manager04.entity.Menu;
import com.bs.manager04.service.IMenuService;
import com.bs.manager04.utils.Result;
import com.bs.manager04.utils.auth.Auth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 菜单管理表 前端控制器
 * </p>
 *
 * @author yao
 * @since 2022-12-17
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "菜单管理系统")
public class MenuController {

    @Resource
    IMenuService service;

    //菜单分页条件查询
    @GetMapping
    @ApiOperation("菜单分页条件查询")
    @Auth("system:menu")
    @PreAuthorize("@auth.hasAuth('system:menu')")
    public Result getIndexMenu(@ApiParam("当前页") @RequestParam(defaultValue = "1") Integer pageNum, @ApiParam("页面大小") @RequestParam(defaultValue = "5") Integer pageSize,@ApiParam("菜单名")  @RequestParam(defaultValue = "") String name){
        Page<Menu> page = service.getIndex(pageNum, pageSize, name);
        return Result.success(page);
    }

    //菜单添加
    @PostMapping
    @ApiOperation("菜单添加")
    @Auth("system:menu:add")
    public Result addMenu(@ApiParam("菜单表")@RequestBody Menu menu){
        boolean save = service.save(menu);
        if(save){
         return Result.success("添加菜单信息成功");
        }else{
        return Result.fail("添加菜单信息失败");
        }
    }

    //根据菜单id查询菜单列表
    @GetMapping("/{id}")
    @ApiOperation("根据菜单id查询菜单列表")
    @Auth("system:menu")
    public Result getByMenuID(@PathVariable("id") @ApiParam("菜单id") String id){
        Menu menu = service.getById(id);
        return Result.success(menu);
    }

    //菜单修改
    @PutMapping
    @ApiOperation("菜单修改")
    @Auth("system:menu:update")
    public Result updateMenu(@RequestBody @ApiParam("菜单表") Menu menu){
        boolean save = service.updateById(menu);
        if(save){
            return Result.success("修改菜单信息成功");
        }else{
            return Result.fail("修改菜单信息失败");
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation("菜单删除")
    @Auth("system:menu:del")
    public Result delMenu(@PathVariable("id") @ApiParam("菜单id") String id){
        boolean del = service.removeById(id);
        if(del){
            return Result.success("删除菜单信息成功");
        }else{
            return Result.fail("删除菜单信息失败");
        }
    }

    @GetMapping("/getByTree")
    @ApiOperation("根据父级id查询菜单列表")
    @Auth("system:menu")
    public Result getByMenuPid(@ApiParam("父级菜单id") @RequestParam(defaultValue = "0") String pid){
        if(pid==null){
            pid="0";
        }
        List<Menu> menus = service.getByMenuPid(pid);
        return Result.success(menus);
    }
 }

